package com.molichuxing.gateway.api.services.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.config.OptionalLogConfig;
import com.molichuxing.gateway.api.services.UserApiService;
import com.molichuxing.gateway.bff.user.entity.request.create.EnterpriseCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.modify.ComplaintModifyVo;
import com.molichuxing.gateway.bff.user.entity.request.modify.DrivingLicenseModifyVo;
import com.molichuxing.gateway.bff.user.entity.request.modify.EnterpriseModifyVo;
import com.molichuxing.gateway.bff.user.entity.response.ComplaintVo;
import com.molichuxing.gateway.bff.user.entity.response.DrivingLicenseVo;
import com.molichuxing.gateway.bff.user.entity.response.EnterpriseVo;
import com.molichuxing.gateway.bff.user.entity.response.IdentityCardVo;
import com.molichuxing.gateway.bff.user.entity.response.UserVo;
import com.molichuxing.gateway.bff.user.services.ComplaintBffService;
import com.molichuxing.gateway.bff.user.services.DrivingLicenseBffService;
import com.molichuxing.gateway.bff.user.services.UserBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author liyg
 * 用户web接口实现类
 * 2019-08-26
 *
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping(value = "/user")
public class UserApiServiceImpl extends RootApiService implements UserApiService {

	private static final Logger logger = LoggerFactory.getLogger(UserApiServiceImpl.class);

	@Resource
	private UserBffService userBffService;
	
	@Resource
	private DrivingLicenseBffService drivingLicenseBffService;
	
	@Resource
	private ComplaintBffService complaintBffService;
	
	
	/**
	 *获取用户列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取用户列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "用户名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "realName", value = "用户姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "phone", value = "用户手机号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "source", value = "用户来源", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "startTime", value = "注册开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "注册结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "isOrder", value = "是否有订单(0：否 1：有)", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "isBlack", value = "是否拉黑 (0：否 1：是 )", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/user/list")
	public ResponseResult<Paged<UserVo>> getUserList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<UserVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize", 10);
			}
			Integer isOrder = ObjectCastUtil.castInteger(map.get("isOrder"));
			if (null != isOrder) {
				if (isOrder == 0) {
					map.put("isOrder", false);
				} else if (isOrder == 1) {
					map.put("isOrder", true);
				}
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if (StringUtils.isNoneBlank(startTime)) {
				map.put("startTime", DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if (StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}


			return result.success(userBffService.getUserList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getUserList]获取用户列表信息异常", e);
			result.error("获取用户列表信息异常");
		}
		return result;
	}
	
	
	/**
	 *根据用户id获取用户信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据用户id获取用户信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/user/detail")
	public ResponseResult<UserVo> getUserById(@RequestParam Integer userId) {
		
		ResponseResult<UserVo> result = new ResponseResult<>();
		try {
			return result.success(userBffService.getUserById(userId));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getUserById]根据用户id获取用户信息异常", e);
			result.error("根据用户id获取用户信息异常");
		}
		return result;
	}
	
	/**
	 *根据订单编号获取用户信息 
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据订单编号获取用户信息 ")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderCode", value = "订单编号", required = true, dataType = "Long")
	})
	@GetMapping(value = "/user/order/detail")
	public ResponseResult<UserVo> getUserByOrderCode(@RequestParam Long orderCode) {
		
		ResponseResult<UserVo> result = new ResponseResult<>();
		try {
			return result.success(userBffService.getUserByOrderCode(orderCode));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getUserByOrderCode]根据订单编号获取用户信息异常", e);
			result.error("根据订单编号获取用户信息异常");
		}
		return result;
	}
	
	/**
	 *获取用户身份证信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取用户身份证信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/user/identity/detail")
	public ResponseResult<IdentityCardVo> getIdentityCard(@RequestParam Integer userId) {
		
		ResponseResult<IdentityCardVo> result = new ResponseResult<>();
		try {
			return result.success(userBffService.getIdentityCard(userId));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getIdentityCard]获取用户身份证信息异常", e);
			result.error("获取用户身份证信息异常");
		}
		return result;
	}
	
	/**
	 *获取驾驶证列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取驾驶证列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userName", value = "用户名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "phone", value = "用户手机号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "驾驶证姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "status", value = "验证状态(0: 验证失败 ，1：验证通过，2：待验证) ", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/drivinglicence/list")
	public ResponseResult<Paged<DrivingLicenseVo>> getDrivingLicenseList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<DrivingLicenseVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			return result.success(drivingLicenseBffService.getDrivingLicenseList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getDrivingLicenseList]获取驾驶证列表信息异常", e);
			result.error("获取驾驶证列表信息异常");
		}
		return result;
	}
	
	/**
	 *获取用户驾驶证信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取用户驾驶证信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/drivinglicence/detail")
	public ResponseResult<DrivingLicenseVo> getDrivingLicense(@RequestParam Integer userId) {
		
		ResponseResult<DrivingLicenseVo> result = new ResponseResult<>();
		try {
			return result.success(drivingLicenseBffService.getDrivingLicense(userId));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getDrivingLicense]获取用户驾驶证信息异常", e);
			result.error("获取用户驾驶证信息异常");
		}
		return result;
	}
	
	/**
	 * 编辑审核状态
	 * @param modifyVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "编辑审核状态")
	@OptionalLogConfig(methods="驾驶证验证")
	@PostMapping(value = "/drivinglicence/modify")
	public ResponseResult<Boolean> modifyVerifyStatus(@RequestBody DrivingLicenseModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			
			Boolean flag = drivingLicenseBffService.modifyVerifyStatus(modifyVo);
			if(!flag) {
				result.error("审核失败");
			}
			return result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyComplaint]编辑审核状态异常", e);
			result.error("编辑审核状态异常");
		}
		return result;
	}
	
	/**
	 *获取投诉列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取投诉列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "realName", value = "用户姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "phone", value = "用户手机号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "投诉方式", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "投诉处理状态", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "startTime", value = "投诉开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "投诉结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/complaint/list")
	public ResponseResult<Paged<ComplaintVo>> getComplaintList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<ComplaintVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			
			return result.success(complaintBffService.getComplaintList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getComplaintList]获取投诉列表信息异常", e);
			result.error("获取投诉列表信息异常");
		}
		return result;
	}
	
	
	/**
	 *获取用户投诉详情
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取用户投诉详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "投诉id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/complaint/detail")
	public ResponseResult<ComplaintVo> getComplaint(@RequestParam Integer id) {
		
		ResponseResult<ComplaintVo> result = new ResponseResult<>();
		try {
			return result.success(complaintBffService.getComplaint(id));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getComplaint]获取用户投诉详情信息异常", e);
			result.error("获取用户投诉详情信息异常");
		}
		return result;
	}
	
	/**
	 * 编辑投诉信息
	 * @param modifyVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "编辑投诉信息")
	@OptionalLogConfig(methods="投诉处理")
	@PostMapping(value = "/complaint/modify")
	public ResponseResult<Boolean> modifyComplaint(@RequestBody ComplaintModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(complaintBffService.modifyComplaint(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyComplaint]编辑投诉信息异常", e);
			result.error("编辑投诉信息异常");
		}
		return result;
	}
	
	
	/**
	 *获取企业用户列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取企业用户列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "企业名称", required = false, dataType = "String"),
			@ApiImplicitParam(name = "regionId", value = "城市id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "startTime", value = "注册开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "注册结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
			@ApiImplicitParam(name = "sortWay", value = "排序方式", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/enterprise/list")
	public ResponseResult<Paged<EnterpriseVo>> getEnterpriseList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<EnterpriseVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			return result.success(userBffService.getEnterpriseList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEnterpriseList]获取企业用户列表信息异常", e);
			result.error("获取企业用户列表信息异常");
		}
		return result;
	}
	
	/**
	 *根据企业用户id获取企业用户详情
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据企业用户id获取企业用户详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "企业用户id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/enterprise/detail")
	public ResponseResult<EnterpriseVo> getEnterpriseById(@RequestParam Integer id) {
		
		ResponseResult<EnterpriseVo> result = new ResponseResult<>();
		try {
			return result.success(userBffService.getEnterpriseById(id));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEnterpriseById]根据企业用户id获取企业用户详情信息异常", e);
			result.error("根据企业用户id获取企业用户详情信息异常");
		}
		return result;
	}
	
	/**
	 * 修改企业用户信息
	 * @param modifyVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "修改企业用户信息")
	@OptionalLogConfig(methods="修改企业用户信息")
	@PostMapping(value = "/enterprise/modify")
	public ResponseResult<Boolean> modifyEnterprise(@RequestBody @Valid EnterpriseModifyVo modifyVo , BindingResult bindingResult){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = userBffService.modifyEnterprise(modifyVo);
			if(flag) {
				return result.success(flag);
			}
			return result.error("修改企业用户信息失败");
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyEnterprise]修改企业用户信息异常", e);
			result.error("修改企业用户信息异常");
		}
		return result;
	}
	
	/**
	 * 新增企业用户信息
	 * @param modifyVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "新增企业用户信息")
	@OptionalLogConfig(methods="新增企业用户信息")
	@PostMapping(value = "/enterprise/create")
	public ResponseResult<Boolean> createEnterprise(@RequestBody @Valid EnterpriseCreateVo createVo, BindingResult bindingResult){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = userBffService.createEnterprise(createVo);
			if(flag) {
				return result.success(flag);
			}
			return result.error("新增企业用户信息失败");
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[createEnterprise]新增企业用户信息异常", e);
			result.error("新增企业用户信息异常");
		}
		return result;
	}
	
	/**
	 * 模糊查询企业用户信息列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "模糊查询企业用户信息列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "企业用户名称", required = false, dataType = "String")
	})
	@GetMapping(value = "/enterprise/base/list")
	public ResponseResult<List<EnterpriseVo>> getEnterpriseBaseList(@RequestParam String name) {
		
		ResponseResult<List<EnterpriseVo>> result = new ResponseResult<>();
		try {
			return result.success(userBffService.getEnterpriseBaseList(name));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEnterpriseBaseList]模糊查询企业用户信息列表异常", e);
			result.error("模糊查询企业用户信息列表异常");
		}
		return result;
	}
	
}
